package com.gxw.rxjavatest.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.gxw.rxjavatest.Presenter.SecPresenter;
import com.gxw.rxjavatest.Presenter.UpImagePresenter;
import com.gxw.rxjavatest.R;
import com.gxw.rxjavatest.activity.View.SecView;
import com.gxw.rxjavatest.activity.View.UpImageView;
import com.gxw.rxjavatest.data.ApiService;
import com.gxw.rxjavatest.model.bean.LogInSec;
import com.gxw.rxjavatest.model.bean.LoginID;
import com.gxw.rxjavatest.model.bean.UpLogin;
import com.gxw.rxjavatest.utils.BitToStr;
import com.gxw.rxjavatest.utils.BitmapScale;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.guava.GuavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

@ContentView(R.layout.activity_second)
public class SecondActivity extends AppCompatActivity implements SecView, UpImageView {

    private String TAG = SecondActivity.class.getName().toString();

    @ViewInject(R.id.fab)
    private FloatingActionButton fab;

    @ViewInject(R.id.tv_sec_name)
    private TextView tv_sec_name;

    @ViewInject(R.id.toolbar_sec)
    private Toolbar toolbar;

    private SecPresenter secPresenter;
    private UpImagePresenter upImagePresenter;
    private UpLogin upLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        setSupportActionBar(toolbar);

        secPresenter = new SecPresenter(this);
        upImagePresenter = new UpImagePresenter(this);
        upLogin = new UpLogin("gxw", "cnea2015", "android");

    }

    @Event(value = {R.id.fab_sec, R.id.btn_second_to_f, R.id.btn_second_to_s, R.id.btn_second_to_t}, type = View.OnClickListener.class)
    private void onClicked(View v) {


        switch (v.getId()) {

            case R.id.btn_second_to_t:

//                Bitmap bitmap = BitmapFactory.decodeResource(SecondActivity.this.getResources(), R.mipmap.img_f);
                Bitmap bitmap = BitmapScale.ReadBitmapById(SecondActivity.this, R.mipmap.img_f);
                Bitmap bitmap1 = BitmapScale.compressImage(bitmap);
                String imgF = BitToStr.initBitToStr(bitmap1);
                upImagePresenter.upImage(upLogin, imgF, 1);

                break;

            case R.id.btn_second_to_f:

                secPresenter.getSecName(upLogin);

//                Gson gson = new Gson();
//                String params = gson.toJson(upLogin);
//                Log.i(TAG, params);

//                Retrofit retrofit = new Retrofit.Builder()
//                        .baseUrl("http://117.79.231.125:17080")
//                        .addConverterFactory(GsonConverterFactory.create())
//                        .addCallAdapterFactory(GuavaCallAdapterFactory.create())
//                        .build();
//                ApiService apiService = retrofit.create(ApiService.class);
//                apiService.getInfo(upLogin).enqueue(new Callback<LogInSec>() {
//                    @Override
//                    public void onResponse(Call<LogInSec> call, Response<LogInSec> response) {
//
//                        Log.i(TAG, "response code:" + response.code());
//                        if (response.body() == null) {
//
//                            Log.i(TAG, " ------------------------------- response.body is null ----------------------------- ");
//
//                        } else {
//                            Log.i(TAG, response.body().toString());
//                        }
//
//                    }
//
//                    @Override
//                    public void onFailure(Call<LogInSec> call, Throwable t) {
//
//                    }
//                });


                break;
            case R.id.fab_sec:
                Snackbar.make(v, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
                break;

            case R.id.btn_second_to_s:

                Retrofit retrofit_s = new Retrofit.Builder()
                        .baseUrl("http://117.79.231.125:17080")
                        .addConverterFactory(GsonConverterFactory.create())
                        .addCallAdapterFactory(GuavaCallAdapterFactory.create())
                        .build();

                ApiService apiService1 = retrofit_s.create(ApiService.class);
                Call<LogInSec> info2 = apiService1.getInfo2(upLogin.getLoginID(), upLogin.getPassword(), upLogin.getDeviceType());
                info2.enqueue(new Callback<LogInSec>() {
                    @Override
                    public void onResponse(Call<LogInSec> call, Response<LogInSec> response) {

                        Log.i(TAG, "response2 code:" + response.code());
                        if (response.body() == null) {
                            Log.i(TAG, " ------------------------------- response2.body is null ----------------------------- ");
                        } else {
                            Log.i(TAG, "response2:" + response.body().toString());
                        }

                    }

                    @Override
                    public void onFailure(Call<LogInSec> call, Throwable t) {

                    }
                });

                break;

        }


    }


    @Override
    public void updateName(LoginID loginID) {
        tv_sec_name.setText(loginID.getName());
    }

    @Override
    public void upImageStateF(String isSuccess) {

        Log.i(TAG, "-- F --" + isSuccess);
        Bitmap bitmap = BitmapScale.ReadBitmapById(SecondActivity.this, R.mipmap.img_s);
        Bitmap bitmap1 = BitmapScale.compressImage(bitmap);
        String imgS = BitToStr.initBitToStr(bitmap1);
        upImagePresenter.upImage(upLogin, imgS, 2);
    }

    @Override
    public void upImageStateS(String isSuccess) {
        Log.i(TAG, "-- S --" + isSuccess);
        Bitmap bitmap = BitmapScale.ReadBitmapById(SecondActivity.this, R.mipmap.img_t);
        Bitmap bitmap1 = BitmapScale.compressImage(bitmap);
        String imgT = BitToStr.initBitToStr(bitmap1);
        upImagePresenter.upImage(upLogin, imgT, 3);
    }

    @Override
    public void upImageStateT(String isSuccess) {
        Log.i(TAG, "-- T --" + isSuccess);
        Bitmap bitmap = BitmapScale.ReadBitmapById(SecondActivity.this, R.mipmap.img_for);
        Bitmap bitmap1 = BitmapScale.compressImage(bitmap);
        String imgF = BitToStr.initBitToStr(bitmap1);
        upImagePresenter.upImage(upLogin, imgF, 4);
    }

    @Override
    public void upImageStateFor(String isSuccess) {
        Log.i(TAG, "-- For --" + isSuccess);
        tv_sec_name.setText(isSuccess);
    }
}
